package workbench.service.impl;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import settings.DAO.UserDao;
import settings.domain.User;
import util.DateUtil;
import workbench.VO.ListAndObject;
import workbench.dao.ActivityDao;
import workbench.dao.ActivityRemarkDao;
import workbench.domain.Activity;
import workbench.domain.ActivityRemark;
import workbench.exception.DeleteException;
import workbench.exception.SaveRemarkException;
import workbench.exception.UpdateException;
import workbench.service.ActivityService;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityRemarkDao activityRemarkDao;
    @Autowired
    private ActivityDao activityDao;
    @Autowired
    private UserDao userDao;
    public List selectOwners(){
        return activityDao.selectOwners();
    }
    public boolean addActivity(Activity activity){
        int count=activityDao.addActivity(activity);
        return count==1;
    }

    @Override
    public List<Activity> getActivityList(int pageNum, int pageSize,Activity activity) {
        PageHelper.startPage(pageNum, pageSize);
        return activityDao.getActivityList(activity);
    }
    public int countActivity(Activity activity){
        return activityDao.countActivity(activity);
    }
    @Transactional
    public boolean deleteActivity(String[] ids) throws DeleteException {
        List<String> list=new ArrayList<>();
        for(String s:ids){
            list.add(s);
        }
        int relationalRecordNum=activityDao.selectRelationalRecord(list);
        int deleteRelationalRecordNum=activityDao.deleteRelationalRecord(list);
        if(relationalRecordNum!=deleteRelationalRecordNum){
            throw new DeleteException("未能删除所有的相关数据");
        }
        int deleteActivityNum=activityDao.deleteActivity(list);
        if(deleteActivityNum!=ids.length){
            throw new DeleteException("未能删除所有的相关数据");
        }else {
            return true;
        }
    }
    public ListAndObject<Activity> getUserListAndActivity(String id){
        List<User> userList=userDao.getUserList();
        Activity activity = activityDao.getActivityById(id);
        return new ListAndObject<>(userList,activity);
    }
    @Transactional
    public void updataActivity(HttpServletRequest req, Activity activity) throws UpdateException {
        User user= (User) req.getSession().getAttribute("userMessage");
        activity.setEditBy(user.getName());
        activity.setEditTime(DateUtil.dateToString19Char(new Date(System.currentTimeMillis())));
        int count=activityDao.updataActivity(activity);
        if(count!=1){
            throw new UpdateException("更新数据失败");
        }
    }
    public Activity detail(String id){
        return activityDao.getActivityById(id);
    }
    public List<ActivityRemark> getRemarkMessageById(String id){
        return activityRemarkDao.getRemarkMessageById(id);
    }
    public void saveRemark(ActivityRemark activityRemark) throws SaveRemarkException {
        int record=activityRemarkDao.saveRemark(activityRemark);
        if(record==1){
            return;
        }else {
            throw new SaveRemarkException("插入remark失败");
        }
    }
}
